﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14_Observer
{
    /*
        针对抽象编程的实现
         */
    interface Subject
    {
        void Attach(AbstractObserver observer);
        void Detach(AbstractObserver observer);
        void Notify();
        string SubjectState
        {
            get;set;
        }
    }

    /*抽象的观察者*/
    abstract class AbstractObserver
    {
        protected string name;
        public abstract void Update();
    }

    /* 股票观察者 */
    class StockObserverAb : AbstractObserver
    {
        public StockObserverAb(string name)
        {
            this.name = name;
        }

        public override void Update()
        {
            Console.WriteLine("{0} 关掉股票页面，开始工作!", name);
        }
    }

    /*抽象的触发器*/
    class Boos: Subject
    {
        protected IList<AbstractObserver> observers = new List<AbstractObserver>();

        public string SubjectState { get ; set; }

        public void Attach(AbstractObserver observer)
        {
            observers.Add(observer);
        }

        public void Detach(AbstractObserver observer)
        {
            observers.Remove(observer);
        }

        public void Notify()
        {
            foreach (var observer in observers)
            {
                observer.Update();
            }
        }
    }
}
